package com.example.himalaya.presenters;

import com.example.himalaya.interfaces.IAlbumDetailPresenter;
import com.example.himalaya.interfaces.IAlbumDetailViewCallback;
import com.example.himalaya.utils.Constans;
import com.example.himalaya.utils.LogUtils;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.BatchAlbumList;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlbumDetialPresenter implements IAlbumDetailPresenter {

    private Album mTargetAlbum = null;
    private List<IAlbumDetailViewCallback> mCallbacks = new ArrayList<>();
    private String TAG ="AlbumDetialPresenter";


    private AlbumDetialPresenter() {
    }

    private static AlbumDetialPresenter sInstance = null;

    public static synchronized AlbumDetialPresenter getsInstance() {
        if (sInstance == null) {
            sInstance = new AlbumDetialPresenter();
        }
        return sInstance;
    }

    @Override
    public void pull2RefreshMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void getAlbumDetail(int albumId, int page) {
        //根据页码和专辑ID获取专辑列表
        Map<String, String> map = new HashMap<String, String>();
        map.put(DTransferConstants.ALBUM_ID, albumId+"");
        map.put(DTransferConstants.SORT, "asc");
        map.put(DTransferConstants.PAGE, page+"");
        map.put(DTransferConstants.PAGE_SIZE, Constans.COUNT_DEFAULT+"");
        CommonRequest.getTracks(map, new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(TrackList trackList) {
                LogUtils.d(TAG,"current Thread" + Thread.currentThread().getName());
                if (trackList != null) {
                    List<Track> tracks =trackList.getTracks();
                    LogUtils.d(TAG,"tracks"+tracks.size());
                    handlerAlbumDetailResult(tracks);

                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtils.d(TAG,"errorCods------>"+i);
                LogUtils.d(TAG,"errorMsg------>"+s);
                handlerError(i,s);
            }
        });
    }

    /**
     * 如果网络发生错误  ，通知UI
     * @param i
     * @param s
     */
    private void handlerError(int i, String s) {
        for (IAlbumDetailViewCallback callback : mCallbacks) {
                callback.onNetWorkError(i,s);
        }
    }

    private void handlerAlbumDetailResult(List<Track> tracks) {
        for (IAlbumDetailViewCallback callback : mCallbacks) {
            callback.onDetailListLoaded(tracks);
        }
    }

    @Override
    public void registerViewCallback(IAlbumDetailViewCallback detailPresenter) {
        if (!mCallbacks.contains(detailPresenter)){
            mCallbacks.add(detailPresenter);
            if (mTargetAlbum != null) {
            detailPresenter.onAlbumLoaded(this.mTargetAlbum);
            }
        }
    }

    @Override
    public void unRegisterViewCallback(IAlbumDetailViewCallback detailPresenter) {
        if ( mCallbacks.contains(detailPresenter)){
            mCallbacks.remove(detailPresenter);
        }
    }

    public void setTargetAlbum(Album targetAlbum) {
        this.mTargetAlbum = targetAlbum;
    }
}
